package com.sky.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.constant.CacheConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.Result;
import com.sky.service.IDishService;
import com.sky.service.ISetmealDishService;
import com.sky.service.ISetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
public class SetmealController {
    @Autowired
    private ISetmealService iSetmealService;
    @Autowired
    private ISetmealDishService iSetmealDishService;
    @Autowired
    private IDishService iDishService;


    @GetMapping("/list")
    @Cacheable(cacheNames = CacheConstant.SETMEAL_CACHE,key = "#dto.categoryId")
    public Result list(SetmealDTO dto){
            //根据每个套餐id，在关系表中找到对应的菜系
            LambdaQueryWrapper<Setmeal> lqw1 =new LambdaQueryWrapper<>();
            lqw1.eq(dto.getCategoryId()!=null,Setmeal::getCategoryId,dto.getCategoryId());
            List<Setmeal> list = iSetmealService.list(lqw1);
        return Result.success(list);
    }

    @GetMapping("/dish/{setmealId}")
    public Result getDish(@PathVariable Long setmealId){
        Setmeal setmeal = iSetmealService.getById(setmealId);
        LambdaQueryWrapper<SetmealDish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = iSetmealDishService.list(lqw);
        List<DishItemVO> voList =new ArrayList<>();
        list.forEach(s->{
            Dish dish = iDishService.getById(s.getDishId());
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(dish,dishItemVO);
            BeanUtils.copyProperties(s,dishItemVO);
            voList.add(dishItemVO);
        });
        //List<dishItemVo>
        return Result.success(voList);
    }

}
